package com.smallA.controller;

import com.smallA.constant.DataConstant;
import com.smallA.po.Exam;
import com.smallA.po.TopicType;
import com.smallA.service.ExamService;
import com.smallA.service.QuestionService;
import com.smallA.service.TestService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TestController {

    private TestService testService;
    private ExamService examService;
    private QuestionService questionService;

    public void setQuestionService(QuestionService questionService) {
        this.questionService = questionService;
    }

    public void setExamService(ExamService examService) {
        this.examService = examService;
    }

    public void setTestService(TestService testService) {
        this.testService = testService;
    }

    @RequestMapping("/Test/getTopicType")
    @ResponseBody
    public Object getTopicType(Model model) {
        List<TopicType> topicTypes = testService.selectAllTopicTpe();
        /*model.addAttribute(DataConstant.TOPIC_TYPE_DATA, topicTypes);
        return "student/main";*/
        return topicTypes;
    }

    @RequestMapping("/Test/autoTest")
    @ResponseBody
    public Object autoTest(Integer teacherId, Model model, Integer[] topicTypeIds, Integer choiceCount, Integer fillCount, Integer shortAnswerCount, String context, int score, HttpSession session) {
        //  Client user = (Client) session.getAttribute(DataConstant.STUDENT_USER);
        Exam exam = examService.autoExam(topicTypeIds, choiceCount, fillCount, shortAnswerCount, context, teacherId, score);
        return exam;
    }

    /**
     * 这个就是单纯获取下试卷的数据然后展示出来而已
     *
     * @param model
     * @return
     */
    @RequestMapping("/Test/getExam")
    @ResponseBody
    public Object getExam(Model model) {
        List<Exam> list = examService.selectAllExam();
       /* model.addAttribute(DataConstant.EXAM_LIST, list);
        return "student/allExam";*/
        return list;
    }

    /**
     * 通过id获取试卷的详细信息
     *
     * @param eid
     * @return
     */
    @RequestMapping("/Test/getExamById")
    @ResponseBody
    public Object getExamById(Integer eid) {
        if (eid == null) {
            return null;
        }
        return examService.selectExamByExamId(eid);
    }

    @RequestMapping("/Test/addTest")
    @ResponseBody
    public Object addTest(Model model, @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginTime, Integer examId, Integer useTime, String context, Integer detailBegin) {
        //开始操作
        Date date = new Date(beginTime.getTime() + detailBegin * 1000 * 60 * 60);
        //这个是获取具体时间,如果可以直接获取对应时间的话这个就可以删除了
        /*if (testService.insertExam(date, examId, useTime, context)) {
            model.addAttribute(DataConstant.MSG, "添加考试成功");
        } else {
            model.addAttribute(DataConstant.MSG, "添加考试失败");
        }
        return "student/other";*/
        return testService.insertTest(date, examId, useTime, context);
    }

    @RequestMapping("/Test/markExam")
    @ResponseBody
    public Object markExam(int gid){
        Integer tid = testService.selectTestByGradeId(gid);
//        Test test = testService.selectTestById(tid);
        Exam exam = examService.selectExamById(gid);
//        model.addAttribute(DataConstant.EXAM_DETAIL, exam);
//        model.addAttribute(DataConstant.ID_GRADE,gid);
//        model.addAttribute(DataConstant.ID_EXAM,test.getExamId());
        Map<String,Object> map = new HashMap<>();
        map.put(DataConstant.EXAM_DETAIL, exam);
        map.put(DataConstant.ID_GRADE,gid);
        return map;

    }

    @RequestMapping("/Test/calculate")
    @ResponseBody
    public boolean calculate(Float[] fills,Float[] shorts,int gid) {
        Exam exam = examService.selectExamById(gid);
        return testService.markGrade(fills,shorts, exam, gid);

    }

    /**
     * 上传excel表格用方法
     *
     * @param file    前端传来的excel表格文件
     * @param request 必要参数
     * @return
     * @throws IOException
     */
    @RequestMapping("/Test/upload")
    @ResponseBody
    public Object ExcelUpload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
        /*上传路径保存设置*/
        String path = request.getServletContext().getRealPath("/upload");
        /*request.setAttribute(DataConstant.MSG, questionService.insertQuestion(path, file));
        return "student/other";*/
        return questionService.insertQuestion(path, file);
    }

    @RequestMapping("/Test/downloadTemplate")
    public void downloads(HttpServletResponse resp, HttpServletRequest req) throws Exception {
        resp = questionService.dowmloadTemplate(resp, req);
    }

}
